package com.jcark.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.jcark.ui.R;
import com.jcark.ui.utils.ViewUtils;

/**
 * 通用dialog
 */
public class CommonDialog extends Dialog {
    private Context context;
    private TextView tv_title;
    private TextView tv_des;
    private TextView tv_cancel;
    private TextView tv_confirm;
    private OnConFirmClickListener onConFirmClickListener;
    private OnCanCelClickListener onCanCelClickListener;

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.context = context;
        initView();
    }

    public interface OnConFirmClickListener {
        void onClick();
    }

    public interface OnCanCelClickListener {
        void onClick();
    }

    public CommonDialog setOnCanCelClickListener(OnCanCelClickListener onCanCelClickListener) {
        this.onCanCelClickListener = onCanCelClickListener;
        return this;
    }

    public CommonDialog setOnConFirmClickListener(OnConFirmClickListener onConFirmClickListener) {
        this.onConFirmClickListener = onConFirmClickListener;
        return this;
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        setContentView(view);
        tv_title = view.findViewById(R.id.tv_title);
        tv_des = view.findViewById(R.id.tv_des);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = ViewUtils.dpToPx(context, 250f);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        getWindow().setGravity(Gravity.CENTER);
        setCanceledOnTouchOutside(false);

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null != onCanCelClickListener) {
                    onCanCelClickListener.onClick();
                }
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null != onConFirmClickListener) {
                    onConFirmClickListener.onClick();
                }
            }
        });
    }

    public CommonDialog setTitleText(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        return this;
    }

    public CommonDialog setDesText(String des) {
        if (!TextUtils.isEmpty(des)) {
            tv_des.setText(des);
        }
        return this;
    }

    public CommonDialog setCancelText(String cancel) {
        if (!TextUtils.isEmpty(cancel)) {
            tv_cancel.setText(cancel);
        }
        return this;
    }

    public CommonDialog setCancelTextColor(int color) {
        tv_cancel.setTextColor(context.getResources().getColor(color));
        return this;
    }

    public CommonDialog setConfirmText(String confirm) {
        if (!TextUtils.isEmpty(confirm)) {
            tv_confirm.setText(confirm);
        }
        return this;
    }

    public CommonDialog setConfirmTextColor(int color) {
        tv_confirm.setTextColor(context.getResources().getColor(color));
        return this;
    }

    public CommonDialog setDesTextSize(int size) {
        tv_des.setTextSize(TypedValue.COMPLEX_UNIT_SP, size); //SP
        return this;
    }
}
